package com.framework.loippi.api.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.framework.loippi.utils.web.SpringUtils;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class ApiUtils {

	public static final String KEY_CODE = "code";
	public static final String KEY_MSG = "message";
	public static final String KEY_OBJ = "object";
	public static final String KEY_PAGE = "totalPage";
	public static final String DEFAULT_SUCCESS_RESULT_CODE = "1";
	public static final String DEFAULT_FAILURE_RESULT_CODE = "0";

	private static SerializeConfig mapping = new SerializeConfig();
	private static String dateFormat;
	static {
		dateFormat = "yyyy-MM-dd HH:mm:ss";
		mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));
	}

	public static String success() {
		Map map = new HashMap();
		map.put("code", "1");
		map.put("message", SpringUtils.getMessage("1", new Object[0]));
		return JSONObject.toJSONString(map);
	}


	public static String success(int totalPages, Object object) {
		Map map = new HashMap();
		map.put("code", "1");
		map.put("message", SpringUtils.getMessage("1", new Object[0]));
		map.put("object", object);
		map.put("totalPage", Integer.valueOf(totalPages));


		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(map,jsonConfig);
		return json.toString();
	}

	public static String success(String code, Object object) {
		Map map = new HashMap();
		map.put("code", code);
		map.put("message", SpringUtils.getMessage(code, new Object[0]));
		map.put("object", object);


		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(map,jsonConfig);
		return json.toString();
	}

	public static String success(Object object) {
		Map map = new HashMap();
		map.put("code", "1");
		map.put("message", SpringUtils.getMessage("1", new Object[0]));
		map.put("object", object);


		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(map,jsonConfig);
		return json.toString();

	}

	public static String error() {
		Map map = new HashMap();
		map.put("code", "0");
		map.put("message", SpringUtils.getMessage("0", new Object[0]));
		return JSONObject.toJSONString(map);
	}

	public static String error(String code, String message) {
		Map map = new HashMap();
		map.put("code", code);
		map.put("message", message);
		return JSONObject.toJSONString(map);
	}

	public static String error(String message) {
		Map map = new HashMap();
		map.put("code", "0");
		map.put("message", message);
		return JSONObject.toJSONString(map);
	}

	public static String error(Integer code, Object[] args) {
		Map map = new HashMap();
		map.put("code", code);
		map.put("message", SpringUtils.getMessage(code.toString(), args));
		return JSONObject.toJSONString(map);
	}
}

class JsonDateValueProcessor implements JsonValueProcessor {  
    private String format ="yyyy-MM-dd HH:mm:ss";  
      
    public JsonDateValueProcessor() {  
        super();  
    }  
      
    public JsonDateValueProcessor(String format) {  
        super();  
        this.format = format;  
    }  
  
    @Override  
    public Object processArrayValue(Object paramObject,  
            JsonConfig paramJsonConfig) {  
        return process(paramObject);  
    }  
  
    @Override  
    public Object processObjectValue(String paramString, Object paramObject,  
            JsonConfig paramJsonConfig) {  
        return process(paramObject);  
    }  
      
      
    private Object process(Object value){  
        if(value instanceof Date){    
//            SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(value); //指定格式
            //return ((Date) value).getTime();  //long类型
        }    
        return value == null ? "" : value.toString();    
    }  
  
}  